package com.purong.purong.activity;

import android.content.Intent;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.widget.Toast;

import com.purong.purong.Contant;
import com.purong.purong.PuRongApp;
import com.purong.purong.bean.LoginBean;
import com.purong.purong.net.ResponeHandle;
import com.purong.purong.util.CommonUtil;
import com.purong.purong.util.SharePreUtil;
import com.yao.sdk.encrypt.MD5;
import com.yao.sdk.net.NetWork;
import com.yao.sdk.net.NetWorkListener;
import com.yao.sdk.net.ResponseInfo;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * Created by Administrator on 2016/8/10.
 */
public class NetActivity extends FragmentActivity implements NetWorkListener {

    public boolean request(final String url, final Map<String, String> params, String token) {
        if (!CommonUtil.isNetConnected(this)) {
            if (this!=null)
            Toast.makeText(this, "网络未连接", Toast.LENGTH_SHORT).show();
            return false;
        }

        NetWork.request(this, url, params, token);
        return true;
    }

    // 网络请求
    public boolean request(final String url, final Map<String, String> params, String token, NetWorkListener listener) {
        if (!CommonUtil.isNetConnected(this)) {
            if (this!=null)
            Toast.makeText(this, "网络未连接", Toast.LENGTH_SHORT).show();
            return false;
        }

        NetWork.request(listener, url, params, token);


        return true;
    }
    public boolean requestNoToast(final String url, final Map<String, String> params, String token) {
        if (!CommonUtil.isNetConnected(this)) {
            return false;
        }

        NetWork.request(this, url, params, token);
        return true;
    }

    // 网络请求
    public boolean requestNoToast(final String url, final Map<String, String> params, String token, NetWorkListener listener) {
        if (!CommonUtil.isNetConnected(this)) {
            return false;
        }

        NetWork.request(listener, url, params, token);


        return true;
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        NetWork.cancelRequest(this);
    }
    public void cancelRequest(){
        NetWork.cancelRequest(this);
    }
    public boolean reInit(NetWorkListener listener) {
//        if (PuRongApp.getLoginInfo(this)!=null) {
//            Log.d("zwj","......islogin");
//            Map params = new LinkedHashMap();
//            params.put("user_phone", PuRongApp.getLoginInfo(this).getPhone());
//            params.put("user_pwd", PuRongApp.getLoginInfo(this).getPwd());
//            if (listener == null) {
//                return request(Contant.LOGIN, params, SharePreUtil.getInstance(this).getToken());
//            } else {
//                return request(Contant.LOGIN, params, SharePreUtil.getInstance(this).getToken(), listener);
//            }
//
//        } else {
        if (PuRongApp.getLoginInfo(this)!=null) {
            LoginBean.remove(this);
        }
        Log.d("zwj","......unlogin");
        Map params = new LinkedHashMap();
        Log.e("zwj", "getIMEI=" + MD5.getMD5Str(CommonUtil.getIMEI(this)));
        params.put("device_no", MD5.getMD5Str(CommonUtil.getIMEI(this)));
        params.put("os", "1");
        params.put("version", CommonUtil.getVersionCode(this));
        if (listener == null) {
            return request(Contant.INITIAL, params, null);
        } else {
            return request(Contant.INITIAL, params, null, listener);
        }

//        }
    }


    @Override
    public void onResponse(final ResponseInfo responseInfo) {
        if (responseInfo.isOk && responseInfo.content != null) {
            responseInfo.resultCode = ResponeHandle.isSuccess(this, responseInfo);
            if (responseInfo.resultCode.equals("0")) {
                responseInfo.isRequestOk = true;
            }
            else if (responseInfo.resultCode.equals("C0002")) {
                reInit(new ReInitListener(responseInfo.url, responseInfo.params));//若返回接口过期，重新初始化或登录获取token
                responseInfo.isOk = false;
            }
        } else if (!responseInfo.isOk) {
            Log.e("zwj", "....请求服务器失败_" + responseInfo.url);
            Log.e("zwj", "....请求服务器失败_" + responseInfo.error.errorInfo);
//            if (this!=null)
//            Toast.makeText(this, "请求服务器失败", Toast.LENGTH_SHORT).show();
        }
    }

    class ReInitListener implements NetWorkListener {
        String url;
        public Map<String, String> params = new LinkedHashMap<>();

        public ReInitListener(String url, Map params) {
            this.url = url;
            this.params.putAll(params);
        }

        @Override
        public void onResponse(ResponseInfo reInitInfo) {
//            reInitInfo.content = new Encrypt().toDecryption(reInitInfo.content);
//            BaseBean bean = JSON.parseObject(reInitInfo.content, BaseBean.class);
            String code = ResponeHandle.isSuccess(NetActivity.this, reInitInfo);
            if (code.equals("0")) {
                request(url, params, SharePreUtil.getInstance(NetActivity.this).getToken());
            } else {
                if (PuRongApp.getLoginInfo(NetActivity.this)!=null) {
                    LoginBean.remove(NetActivity.this);
                    Intent intent = new Intent(NetActivity.this, LoginActivity.class);
                    startActivity(intent);
                }

            }
        }
    }
}
